package org.dicker.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.dicker.dao.UserDao;
import org.dicker.entity.MessageInfo;
import org.dicker.entity.UserInfo;
import org.dicker.util.ORMUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public List<UserInfo> selectAll(String username,int page,int pageSize) {
		String sql="SELECT * FROM tb_user where 1=1"; 
		
		List<Object> valueList = new ArrayList<Object>();
		
		if(username!=null && !username.equals("")){
			sql+=" and user_name like ?";
			String wenhao="%"+username+"%";
			valueList.add(wenhao);
		}
		
		sql+=" ORDER BY user_id LIMIT ?,?";
		valueList.add((page-1)*pageSize);
		valueList.add(pageSize);
		
		return ORMUtil.getInstance().selectList(UserInfo.class, sql,valueList.toArray());
	}

	@Override
	public int update_user(UserInfo userInfo) {
		String sql="update tb_user set user_name=?,user_img=?"
				+ "where user_id=?";
		int upd =ORMUtil.getInstance().executeDML(sql,userInfo.getUser_name(),userInfo.getUser_img(),userInfo.getUser_id());
		return upd;
	}

	@Override
	public int deleteuserid(int user_id) {
		String sql="DELETE FROM tb_user WHERE user_id=?";
		return ORMUtil.getInstance().executeDML(sql, user_id);
	}

	@Override
	public UserInfo selectUserid(String wx_openid) {
		String sql="select * from tb_user where wx_openid =?";	
		List<UserInfo> list = ORMUtil.getInstance().selectList(UserInfo.class, sql, wx_openid);
		
		return list.size()>0?list.get(0):null;
	}

	@Override
	public int selectCount(String username) {
		String sql="SELECT count(*) FROM tb_user where 1=1"; 
		
		List<Object> valueList = new ArrayList<Object>();
		
		if(username!=null && !username.equals("")){
			sql+=" and user_name like ?";
			String wenhao="%"+username+"%";
			valueList.add(wenhao);
		}
		long value=(long) ORMUtil.getInstance().selectSingleValue("count(*)", sql, valueList.toArray());
		return (int) value;
	}

	@Override
	public boolean addUser(UserInfo uInfo) {
		String sql="INSERT INTO tb_user(user_name,user_img,wx_openid) "
				+ " VALUES (?,?,?)";
		return ORMUtil.getInstance().executeDML(sql,uInfo.getUser_name(),uInfo.getUser_img(),uInfo.getWx_openid())>0;
	}

	
	
}
